package org.wang.portal.config;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConfigFileManager {
    private static Map<String, String> config = new HashMap<>();
    public static void updateConfigFile(String filePath, Map<String, String> config) throws IOException {
        StringBuilder content = new StringBuilder();
        for (Map.Entry<String, String> entry : config.entrySet()) {
            content.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
        }
        Files.write(Paths.get(filePath), content.toString().getBytes());
    }

    public static Map<String, String> readConfigFile(String filePath) throws IOException {
        config.clear();
        List<String> lines = Files.readAllLines(Paths.get(filePath));
        for (String line : lines) {
            String[] parts = line.split("=", 2);
            if (parts.length == 2) {
                config.put(parts[0], parts[1]);
            }
        }
        return config;
    }
    public static Map<String, String> getConfig() throws IOException {
        return config;
    }
}
